package com.example.pingtools.base

import android.app.AlertDialog
import android.content.Context
import android.opengl.Visibility
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.core.view.isVisible
import com.example.mygreendao.GreenDaoManager
import com.example.pingtools.R
import com.example.pingtools.bean.FloderBean
import com.example.pingtools.bean.IpDataBean
import com.example.pingtools.bean.isupdate
import com.example.pingtools.util.PingUtil
import com.example.pingtools.util.ShareUtil
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.layout_addip.*
import kotlinx.android.synthetic.main.layout_addip.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import java.lang.Exception
import java.net.ConnectException
import java.net.InetSocketAddress
import java.net.Socket

class confirmdialog(context: Context,iplist : MutableList<IpDataBean>?,list:MutableList<FloderBean>?,item:Int,isselect:Int) : AlertDialog(context),
    View.OnClickListener {
    var iplist:MutableList<IpDataBean>?=null
    var isselect:Int=0
    var item:Int=0
    var list:MutableList<FloderBean>?=null
    init {
        var layout = LayoutInflater.from(context).inflate(R.layout.layout_confrim, null)

        setView(layout)

        //设置点击别的区域关闭页面
        setCancelable(true)

        layout.tv_start.setOnClickListener(this)
        layout.tv_cancle.setOnClickListener(this)
        this.iplist=iplist
        this.isselect=isselect
        this.item=item
        this.list=list
    }

    override fun show() {
        val params: WindowManager.LayoutParams = window!!.attributes
        params.width = 700
        params.gravity = Gravity.CENTER
        window!!.attributes = params
        window!!.setBackgroundDrawableResource(android.R.color.transparent)

        super.show()
    }

    override fun onClick(v: View?) {
        when (v) {
            tv_start ->{
                if(isselect==1){
                    var ip = iplist!!.get(item)
                    GreenDaoManager(context).delip(ip)
                    EventBus.getDefault().postSticky(isupdate(2,false,0))
                }else{
                    var floderbean=list!!.get(item)
                    GreenDaoManager(context).delfloder(floderbean)
                  /*  var spfloderid=ShareUtil.getString("floderid", context)
                    if(spfloderid==floderbean.id.toString()){
                        Log.d("xxxxxxxxxxxx",spfloderid+"---"+floderbean.id)
                        ShareUtil.putString("floderid", null, context)
                        ShareUtil.putString("flodername", null, context)
                    }*/
                    EventBus.getDefault().postSticky(isupdate(1,true,floderbean.id))

                }
                dismiss()
            }
            tv_cancle -> {
                dismiss()
            }
        }

    }

}
